/* 
 * 创建日期 2011-4-19
 *
 * 成都天和软件公司
 * 电话：028-85425861 
 * 传真：028-85425861-8008 
 * 邮编：610041 
 * 地址：成都市武侯区航空路6号丰德万瑞中心B座1001 
 * 版权所有
 */
package  org.fantasy.cpp.core.html.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import  org.fantasy.common.db.bean.Row;

/**
 * 查询结果转换为树支持的数据
 * @author 王文成
 * @version 1.0
 * @since 2011-4-19
 */
public class TreeData {
	
	private List<Row> rows ;
	
	private String leafField = "leaf";
	
	private String idField = "id";
	
	private String textField = "text";
	
	private String parentField = "parent";
	
	/**
	 * 被抛弃的节点
	 */
	private Set<String> outs = new HashSet<String>();
	
	public TreeData(List<Row> rows){
		this.rows = rows;
	}
	
	public TreeData(List<Row> rows , String idField , String textField , String parentField , String leafField){
		this.rows = rows;
		this.idField = idField;
		this.textField = textField;
		this.parentField = parentField;
		this.leafField = leafField;
	}
	
	public String toJSON(boolean async){
		JSONArray root = new JSONArray();
		for( Row row : rows){
			if( async )
				createJSON(root,row);
			else
				createJSONDeep(root, row);
		}
		return root.toString();
	}
	
	/**
	 * 创建节点HTML
	 * @param html
	 * @param row
	 */
	private void createJSON(JSONArray root , Row row){
		String id = row.getString(idField);
		String text = row.getString(textField);
		String parent = row.getString(parentField);
		boolean leaf = row.getBoolean(leafField);
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("text", text);
		obj.put("parent", parent);
		obj.put("state", leaf ? "opened" : "closed");
		root.add(obj);
	}
	
	/**
	 * 创建节点HTML
	 * @param html
	 * @param row
	 */
	private void createJSONDeep(JSONArray root , Row row){
		String id = row.getString(idField);
		String text = row.getString(textField);
		String parent = row.getString(parentField);
		boolean leaf = row.getBoolean(leafField);
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("text", text);
		obj.put("parent", parent);
		obj.put("state", leaf ? "opened" : "closed");
		root.add(obj);
		List<Row> childs = getChilds(row);
		if( !childs.isEmpty()){
			JSONArray array = new JSONArray();
			obj.put("children", array);
			for(Row child : childs){
				createJSON(array,child);
			}
		}
	}
	
	/**
	 * 转换成<UL><li></li></UL>
	 * @return
	 */
	public String toUlLi(){
		StringBuffer html = new StringBuffer(1024);
		for( Row row : rows){
			createUlLi(html,row);
		}
		return html.toString();
	}
	
	/**
	 * 创建节点HTML
	 * @param html
	 * @param row
	 */
	private void createUlLi(StringBuffer html , Row row){
		if( isOut(row) ) return;
		List<Row> childs = getChilds(row);
		String id = row.getString(idField);
		String text = row.getString(textField);
		String parent = row.getString(parentField);
		String state = childs.isEmpty() ? "opened" : "closed";
		html.append("	<li  id='" + id + "' parent='" + parent + "' state='" + state + "'>");
		html.append("		<span>" + text + "</span> \n");
		if( !childs.isEmpty()){
			html.append("	 \n<ul> \n");
			for(Row child : childs)
				createUlLi(html,child);
			html.append("	</ul> \n");
		}
		html.append("	</li> \n");
		addOut(row);
	}
	
	/**
	 * 是否已经Out了
	 * @param row
	 * @return
	 */
	private boolean isOut(Row row){
		return outs.contains(row.getString(idField));
	}
	
	/**
	 * 添加out的节点
	 * @param row
	 * @return
	 */
	private void addOut(Row row){
		outs.add(row.getString(idField));
	}
	
	/**
	 * 取得子节点
	 * @param row
	 * @return
	 */
	private List<Row> getChilds(Row row){
		String id = row.getString(idField);
		List<Row> childs = new ArrayList<Row>();
		for(Row child : rows){
			String parent = child.getString(parentField);
			if( parent.equals(id) )
				childs.add(child);
		}
		return childs;
	}
}
